#include "fuzzyxmlwriter.h"

FuzzyXmlWriter::FuzzyXmlWriter(const QString &fileName):
        XmlWriter(fileName)
{
}

void FuzzyXmlWriter::writeCollection(const Collection& collection) {

    this->writeObject("Collection");

    QString num;
    num.setNum(collection.getSize());
    this->writeAttribute("Size", num);

    for( int i = 0; i < collection.getSize(); ++i )
       this->writeSet(*collection.getSet(i));

    this->closeObject("Collection");

}

void FuzzyXmlWriter::writeSet(const BasicMembershipFunction& set) {

    this->writeObject("Fuzzy_set");

    this->writeAttribute("Type", set.getType());
    this->writeAttribute("Name", set.getName());

    this->writeObject("Point_list");

    QPointF* points = set.getPoints();

    for(int i = 0; i < set.getSize(); ++i ) {
        QString num;
        num.setNum(points[i].x());
        this->writeAttribute("Point", num);
    }

    this->closeObject("Point_list");
    this->closeObject("Fuzzy_set");

}


void FuzzyXmlWriter::writeRules(const QVector<Rule*>& rules) {

    this->writeObject("Rule_list");

    QString num;
    num.setNum(rules.size());
    this->writeAttribute("Size", num);

    for(int i = 0; i < rules.size(); ++i)
        this->writeRule(*rules.at(i));

    this->closeObject("Rule_list");

}

void FuzzyXmlWriter::writeRule(const Rule &rule) {

    this->writeObject("Rule");

    this->writeAttribute("Input_1", rule.getFirstInput());
    this->writeAttribute("Input_2", rule.getSecondInput());
    this->writeAttribute("Output", rule.getOutput());

    this->closeObject("Rule");
}

void FuzzyXmlWriter::beginScenario() {
    this->writeObject("Scenario");
}

void FuzzyXmlWriter::closeScenario() {
    this->closeObject("Scenario");
}
